/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—06—19
 * Time: 18:28
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/cartBookServlet")
public class cartBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out= resp.getWriter();

        int id=Integer.parseInt(req.getParameter("id"));
        ArrayList<Book> bkDB=BookDB.getAll();
        Book bk=bkDB.get(id-1);

        HttpSession session=req.getSession();
        ArrayList<Book> cart=(ArrayList<Book>)session.getAttribute("cart");
        if(cart==null){
            cart=new ArrayList<Book>();
        }
        cart.add(bk);
        session.setAttribute("cart",cart);
        out.println("<h2>添加购物车成功</h2>");
        out.println("<a href=\"listBooks.jsp\">继续添加购物车</a><br><br>");
        out.println("<a href=\"listCart.jsp\">查看购物车</a>");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}